/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/
/* Includes ----------------------------------------------------------------------------*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Bsp.h"
#include "button.h"
#include "device_pins.h"
#include "PowerManager.h"
#include "main.h"
#include "UI.h"
#include "UI_Welcome.h"
#include "oled.h"
#include "MEAS.h"

/* Extern variables --------------------------------------------------------------------*/
/* Private define ----------------------------------------------------------------------*/
/* Private macro -----------------------------------------------------------------------*/
/* Private typedef ---------------------------------------------------------------------*/
/* Private variables -------------------------------------------------------------------*/
static TimerHandle_t ui_timer = NULL;
static StaticTimer_t xui_timer;
static EVENT_SRCEEN event_screen;
static void cb_ui_timer(TimerHandle_t parameter);
static SemaphoreHandle_t mtx_process_event = NULL;     // 互斥量句柄
static StaticSemaphore_t xmtx_process_event;  // 存储互斥量的数据结构

static uint16_t page_change_timer;
static uint16_t instatus;
/* Private functions -------------------------------------------------------------------*/
static HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID);
static HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID);

static HMI_SCREEN_ACTION	UI_action = 
{
	UI_Initialize,
	UI_Prepare,
	UI_Refresh,
	UI_ProcessEvent,
	UI_PostProcess,
	UI_ExitScreen,
};

HMI_SCREEN_OBJECT		UI_Welcome = 
{
	UI_SCREEN_ID_WELCOME,
	&UI_action
};

/*****************************************************************************************
* Function Name  : UI_Welcome_Initialize
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev)
{	
	// rt_err_t result;
    (void)instatus;
	mtx_process_event = xSemaphoreCreateMutexStatic(&xmtx_process_event);
	if(mtx_process_event == NULL)
	{
		while(1);
	}
	
	ui_timer = xTimerCreateStatic("tm_ui_period", pdMS_TO_TICKS(50), pdTRUE, NULL, cb_ui_timer, &xui_timer);
	
	HMI_EVENT_INIT(event_screen);
	
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : UI_ExitScreen
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev)
{	
	xTimerStop(ui_timer, portMAX_DELAY);
	
#if DEV_TYPE == DEV_TYPE_I7
	OLED_Clear();
#elif DEV_TYPE == DEV_TYPE_XP
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);    
#elif DEV_TYPE == DEV_TYPE_MINI
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);
#elif DEV_TYPE == DEV_TYPE_VACUUM

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
    
#elif DEV_TYPE == DEV_TYPE_RPT_STD	
    OLED_Clear();
#else
#error
#endif
	vTaskDelay(pdMS_TO_TICKS(100));
	
	return HMI_RET_NORMAL;
}
//
	
/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters)
{	
//	rt_int32_t light_mode[32];
    uint8_t version_buf[8];	
	xTimerStart(ui_timer, portMAX_DELAY);
	  
    (void)version_buf[0];
    
#if DEV_TYPE == DEV_TYPE_I7
	
	lcd_fillBitmap(0,2,125,61,(uint8_t*)map_Vcan,0);
	version_buf[0]='V';
	version_buf[1]=FW_VER_MAJOR + '0';
	version_buf[2]='.';
	version_buf[3]=FW_VER_MINOR + '0';
	version_buf[4]='.';
	version_buf[5]=FW_VER_REVISION + '0';
	version_buf[6]=FW_VER_LABEL + 'A';
	version_buf[7]='\0';
	OLED_ShowString(80,6,version_buf,8,0);
	OLED_Refresh();
	
#elif DEV_TYPE == DEV_TYPE_XP
	instatus=0;
    
	rses_led_control(&led_sys,LED_COLOR_BLACK);
    rses_led_control(&led_ble,LED_COLOR_BLACK);
    rses_led_control(&led_run,LED_COLOR_BLACK);

#elif DEV_TYPE == DEV_TYPE_MINI

    instatus=0;
    
	rses_led_control(&led_sys,LED_COLOR_BLACK);
    rses_led_control(&led_ble,LED_COLOR_BLACK);
    rses_led_control(&led_run,LED_COLOR_BLACK);
    
#elif DEV_TYPE == DEV_TYPE_VACUUM
    
    instatus=0;
    
	rses_led_control(&led_sys,LED_COLOR_BLACK);
    rses_led_control(&led_ble,LED_COLOR_BLACK);
    rses_led_control(&led_run,LED_COLOR_BLACK);

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

#elif DEV_TYPE == DEV_TYPE_RPT_STD
	
	lcd_fillBitmap(0,2,125,61,(uint8_t*)map_Vcan,0);
	version_buf[0]='V';
	version_buf[1]=FW_VER_MAJOR + '0';
	version_buf[2]='.';
	version_buf[3]=FW_VER_MINOR + '0';
	version_buf[4]='.';
	version_buf[5]=FW_VER_REVISION + '0';
	version_buf[6]=FW_VER_LABEL + 'A';
	version_buf[7]='\0';
	OLED_ShowString(80,6,version_buf,8,0);
	OLED_Refresh();
    
#else
#error
#endif

	page_change_timer = 0;
		
	return HMI_RET_NORMAL;
}
//


/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters)
{
	page_change_timer++;
	
#if DEV_TYPE == DEV_TYPE_I7
	if(page_change_timer>35)
	{      
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
	}
	
#elif DEV_TYPE == DEV_TYPE_XP
	
	if(instatus == 0)
	{
		page_change_timer=0;
		instatus = 1;
        rses_led_control(&led_sys,LED_COLOR_GREEN);
        rses_led_control(&led_ble,LED_COLOR_BLACK);
        rses_led_control(&led_run,LED_COLOR_BLACK);
	}
	else if(instatus == 1)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 2;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLUE);
            rses_led_control(&led_run,LED_COLOR_BLACK);
		}
	}
	else if(instatus == 2)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 3;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_GREEN);
		}
	}
	else if(instatus == 3)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 4;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_RED);
		}
	}
	else if(instatus == 4)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 5;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLUE);
            rses_led_control(&led_run,LED_COLOR_BLACK);
		}
	}
	else if(instatus == 5)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 6;
            rses_led_control(&led_sys,LED_COLOR_RED);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_BLACK);
		}
	}
    else if(instatus == 6)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 7;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_BLACK);
		}
	}
	else if(instatus == 7)
	{
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
	}
    
#elif DEV_TYPE == DEV_TYPE_MINI

	if(instatus == 0)
	{
		page_change_timer=0;
		instatus = 1;
        rses_led_control(&led_sys,LED_COLOR_GREEN);
        rses_led_control(&led_ble,LED_COLOR_BLACK);
        rses_led_control(&led_run,LED_COLOR_BLACK);
	}
	else if(instatus == 1)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 2;
            rses_led_control(&led_sys,LED_COLOR_RED);
            rses_led_control(&led_ble,LED_COLOR_BLUE);
            rses_led_control(&led_run,LED_COLOR_BLACK);
		}
	}
	else if(instatus == 2)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 3;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_GREEN);
		}
	}
	else if(instatus == 3)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 4;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_RED);
		}
	}
	else if(instatus == 4)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 5;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_BLACK);
		}
	}
	else if(instatus == 5)
	{
        HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
    }		
    
#elif DEV_TYPE == DEV_TYPE_VACUUM

    if(instatus == 0)
	{
		page_change_timer=0;
		instatus = 1;
        rses_led_control(&led_sys,LED_COLOR_GREEN);
        rses_led_control(&led_ble,LED_COLOR_BLACK);
        rses_led_control(&led_run,LED_COLOR_BLACK);
	}
	else if(instatus == 1)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 2;
            rses_led_control(&led_sys,LED_COLOR_RED);
            rses_led_control(&led_ble,LED_COLOR_BLUE);
            rses_led_control(&led_run,LED_COLOR_BLACK);
		}
	}
	else if(instatus == 2)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 3;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_GREEN);
		}
	}
	else if(instatus == 3)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 4;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_RED);
		}
	}
	else if(instatus == 4)
	{
		if(page_change_timer>3)
		{
			page_change_timer=0;
			instatus = 5;
            rses_led_control(&led_sys,LED_COLOR_BLACK);
            rses_led_control(&led_ble,LED_COLOR_BLACK);
            rses_led_control(&led_run,LED_COLOR_BLACK);
		}
	}
	else if(instatus == 5)
	{
        while(KEY_POWER_STS()!=0)
		{
			rt_thread_delay(10);
		}		       
        HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
    }	
    
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
	
    if(instatus == 0)
	{
		page_change_timer=0;
		instatus = 1;
//		rses_led_control(&led_sys,LED_COLOR_GREEN);
//        rses_led_control(&led_ble,LED_COLOR_BLACK);
//        rses_led_control(&led_run,LED_COLOR_BLACK);     
	}
	else if(instatus == 1)
	{
		if(page_change_timer>40)
		{
//			page_change_timer=0;
//			instatus = 2;
//			rses_led_control(&led_sys,LED_COLOR_BLACK);
//            rses_led_control(&led_ble,LED_COLOR_BLUE);
//            rses_led_control(&led_run,LED_COLOR_BLACK);
			HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
            
		}
	}
//	else if(instatus == 2)
//	{
//		if(page_change_timer>5)
//		{
//			page_change_timer=0;
//			instatus = 3;
//			rses_led_control(&led_sys,LED_COLOR_BLACK);
//            rses_led_control(&led_ble,LED_COLOR_BLACK);
//            rses_led_control(&led_run,LED_COLOR_GREEN);        
//		}
//	}
//	else if(instatus == 3)
//	{
//		if(page_change_timer>5)
//		{
//			page_change_timer=0;
//			instatus = 4;
//			rses_led_control(&led_sys,LED_COLOR_RED);
//            rses_led_control(&led_ble,LED_COLOR_BLACK);
//            rses_led_control(&led_run,LED_COLOR_BLACK);           
//		}
//	}
//	else if(instatus == 4)
//	{
//		if(page_change_timer>5)
//		{
//			page_change_timer=0;
//			instatus = 5;
//			rses_led_control(&led_sys,LED_COLOR_BLACK);
//            rses_led_control(&led_ble,LED_COLOR_BLUE);
//            rses_led_control(&led_run,LED_COLOR_BLACK);
//		}
//	}
//	else if(instatus == 5)
//	{
//		if(page_change_timer>5)
//		{
//			page_change_timer=0;
//			instatus = 6;
//			rses_led_control(&led_sys,LED_COLOR_BLACK);
//            rses_led_control(&led_ble,LED_COLOR_BLACK);
//            rses_led_control(&led_run,LED_COLOR_GREEN);         
//		}
//	}
//	else if(instatus == 6)
//	{
//		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
//	}

#elif DEV_TYPE == DEV_TYPE_RPT_STD

    if(page_change_timer>35)
	{      
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
	}

#else
#error
#endif
	
	return HMI_RET_NORMAL;
}

/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID)
{
	HMI_ENGINE_RESULT		eProcessResult;
	SGUI_UINT16				KeyValue;
	SGUI_UINT16				KeyAction;
	KEY_PRESS_EVENT			*pKeyEvent;
	SGUI_INT				ProcessAction;
	
	xSemaphoreTake(mtx_process_event, portMAX_DELAY);
	
	
	eProcessResult = HMI_RET_NORMAL;
	ProcessAction = HMI_DEMO_PROC_NO_ACT;
	
	if(EVENT_TYPE_ACTION == pEvent->iType)
	{
		if(EVENT_ID_KEY_PRESS == pEvent->iID)
		{			
			pKeyEvent = (KEY_PRESS_EVENT*)pEvent;
			KeyValue = EVENT_KEY_ID(pKeyEvent->Data.uiKeyValue);
			KeyAction = EVENT_KEY_ACTION(pKeyEvent->Data.uiKeyValue);	
			
			switch(KeyValue)
			{
				case KEY_UP:
				{
					eProcessResult = HMI_RET_CANCEL_KEY;
					if(KeyAction == BUTTON_EVENT_LONG)
					{
						eProcessResult = HMI_RET_CANCEL_KEY;
					}					
				}break;
				
				case KEY_DOWN:
				{
					eProcessResult = HMI_RET_CANCEL_KEY;
					if(KeyAction == BUTTON_EVENT_LONG)
					{
						eProcessResult = HMI_RET_CANCEL_KEY;					
					}
				}break;	
			}
		}
		else if(EVENT_ID_SCREEN == pEvent->iID)
		{
			EVENT_SRCEEN *temp_event;
			temp_event = (EVENT_SRCEEN*)pEvent;
			ProcessAction = HMI_DEMO_PROC_NO_ACT;
					
			if(temp_event->Data == 1)
			{
				UI_Refresh(pDev,NULL);
			}
		}
	}
	else if(EVENT_TYPE_DATA == pEvent->iType)
	{
//		if(EVENT_ID_INFOPANNEL == pEvent->iID)
//		{
//			ProcessAction = HMI_DEMO_PROC_NO_ACT;
//			HMI_SwitchScreen(UI_SCREEN_ID_CALIBRATION,&InfoPannel);
//		}
	}
	
	if(pActionID != NULL)
	{
		*pActionID = ProcessAction;
	}
	
	xSemaphoreGive(mtx_process_event);
	
	return eProcessResult;
}
//

HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID)
{
	if(HMI_PROCESS_SUCCESSFUL(eProcResult))
	{
		if(HMI_DEMO_PROC_CONFIRM == ActionID)
		{
			
		}
		else if(HMI_DEMO_PROC_CANCEL == ActionID)
		{
			HMI_GoBack(NULL);
		}
		
	}
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : cb_timer_autoback
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
static void cb_ui_timer(TimerHandle_t parameter)
{
	event_screen.Head.iType =	EVENT_TYPE_ACTION;
	event_screen.Head.iID 	=	EVENT_ID_SCREEN;
	event_screen.Data = 1;	
	HMI_ProcessEvent((HMI_EVENT_BASE*)&event_screen);
}
//

/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/

